/**
 * <p>Title: DataInterface.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-06-14 下午04:37:53  高强  Create
 *</pre>
 */
package com.baosight.sczx.common.zm.cInterface.ct;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.zm.ct.domain.ZMCTDataInterface;

/**
 * <p>Title: DataInterface.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-06-14 下午04:37:53  高强  Create
 *          2012-06-28		王堃		重构，调整测试连接、sql等方式，增加查询方法
 *</pre>
 */
public class DataInterface {
	
	private Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	private ZMCTDataInterface dataInterface = new ZMCTDataInterface() ;
	private java.sql.Connection con ;
	private ResultSet rs;
	private String fdDriver = "0";
	private String fdUrl = "0";
	private String fdUserName = "0";
	private String fdPassword = "0";
	
	public DataInterface()
	{
		
	}

	public DataInterface(String keyId)
	{
		initData(keyId);//当第一次使用的时候初始化
	}
	
	@SuppressWarnings("unchecked")
	private void initData(String keyId)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", keyId);
		List<ZMCTDataInterface> _list = dao.query("ZMCTDataInterface.query", params);
		if(_list.size() < 1)
		{
			dataInterface = null;
		}
		else
		{
			dataInterface = _list.get(0);
			fdDriver = dataInterface.FdDriver();
			fdUrl = dataInterface.getFdUrl();
			fdUserName = dataInterface.getFdUserName();
			fdPassword = dataInterface.getFdPassword();
		}
		CmesConstants.SYSTEMSETTING_DS = 0;
	}
	
	/**
	 * 获得驱动
	 * <p>Function: getFdDriver</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-06-14 上午10:46:38  高强 
	 * </p>
	 *
	 */
	public String getFdDriver()
	{
		return fdDriver;
	}
	
	/**
	 * 获得URL
	 * <p>Function: getFdUrl</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-06-14 上午10:46:38  高强 
	 * </p>
	 *
	 */
	public String getFdUrl()
	{
		return fdUrl;
	}
	
	/**
	 * 获得用户名
	 * <p>Function: getFdName</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-06-14 上午10:46:38  高强 
	 * </p>
	 *
	 */
	public String getFdUserName()
	{
		return fdUserName;
	}
	
	/**
	 * 获得密码
	 * <p>Function: getFdPassword</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-06-14 上午10:46:38  高强 
	 * </p>
	 *
	 */
	public String getFdPassword()
	{
		return fdPassword;
	}
	/**
	 * 
	 * <p>Function: getDsBlock</p>
	 * <p>Description: 获取数据源信息的block</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-06-14 下午04:37:53  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiBlock getDsBlock(String blockName, Map params)
	{                                                              
		EiBlock block = new EiBlock(blockName);
		
		EiColumn eiColumn = new EiColumn("fdKeyId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdDriver");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdUrl");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdUserName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdPassword");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		block.setRows(getDsList(params));
		return block;
	}
	
	/**
	 * 
	 * <p>Function: getDsList</p>
	 * <p>Description: 获取数据源信息以“List<Map>”形式返回</p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-06-14 下午04:37:53  高强 
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Map> getDsList(Map params)
	{
		List<Map> _list = new ArrayList();
		try
		{
			_list = dao.query("ZMCTDataInterface.queryInterface", params);
		}
		catch (Exception e)
		{
			return _list;
		}
		return _list;
	}
	
	/**
	 * 测试初始驱动是否正确
	 * <p>Function: drive</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public boolean drive()
	{
		try
		{    
			//加载驱动类    
			Class.forName(fdDriver) ;
			return true;
		}
		catch(ClassNotFoundException e)
		{    
			return false;  
	    }    
	}
	
	/**
	 * 测试初始数据库连接是否正确
	 * <p>Function: connect</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public boolean connect()
	{
		try
		{     
			//连接数据库 
			con = DriverManager.getConnection(fdUrl, fdUserName, fdPassword);   
			return true;
		}
		catch(SQLException se)
		{    
			return false;
		}    
	}
	
	/**
	 * 测试查询语句是否正确
	 * <p>Function: search</p>
	 * <p>Description: </p>
	 * <p>return value: ResultSet</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public ResultSet search(String sql)
	{
		try
		{     
			Statement st=con.createStatement();
			rs=st.executeQuery(sql);  
			return rs;
		}
		catch(SQLException se)
		{  
			return null;
		}    
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 根据sql语句查询</p>
	 * <p>return value:ResultSet</p>
	 * <p>History:
	 *    2012-6-28下午01:52:32 wangkun
	 * </p>
	 *
	 */
	public ResultSet query(String sql)
	{
		try
		{
			Class.forName(fdDriver);
			con = DriverManager.getConnection(fdUrl, fdUserName, fdPassword);
			Statement st = con.createStatement();
			rs = st.executeQuery(sql);
			return rs;
		}
		catch(Exception se)
		{  
			return null;
		}    
	}
	
	/**
	 * 关闭数据库连接
	 * <p>Function: colseDs</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-18上午10:00:31  高强  
	 * </p>
	 *
	 */
	public boolean colseDs()
	{
		try
		{
			if(rs != null)
			{
				rs.close();
			}
			else
			{
				if(con != null)
				{
					con.close();
				}
			}
			return true;
		}
		catch(SQLException se)
		{    
			return false;
		}    
	}
}
